package start.doubudouyin.controller;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import start.doubudouyin.entity.Usersmessage;
import start.doubudouyin.service.UsersmessageService;
import start.doubudouyin.utils.Result;
import java.util.List;

@RestController
@RequestMapping("/usersmessage")
@CrossOrigin(origins = "http://localhost:5173")
public class UsersmessageController {

    @Resource
    private UsersmessageService usersmessageService;

    @PutMapping("/sendMessage")
    public Result sendMessage(@RequestBody Usersmessage usersmessage) {
        if (usersmessageService.insertUsersMessage(usersmessage)) {
            return Result.success("消息发送成功");
        }
        return Result.error();
    }

    @GetMapping("/getChatHistory")
    public Result getChatHistory(
            @RequestParam String senderId,
            @RequestParam String receiverId) {
        List<Usersmessage> messages = usersmessageService.getChatHistory(senderId, receiverId);
        return Result.success(messages);
    }
}